<template>
    <MainWnd v-if="_window_type === 'main'" ref="main_wnd"></MainWnd>
    <CaptureWnd v-if="_window_type === 'map'" ref="capture_wnd"></CaptureWnd>
</template>

<script setup>
import { onMounted, onUnmounted, provide, ref, getCurrentInstance } from 'vue';
import MainWnd from '@/components/MainWnd/MainWnd.vue';
import CaptureWnd from '@/components/CaptureWnd/CaptureWnd.vue';

// vue属性定义
const name = 'App';
provide('vueApp', { monitorKeyEvent });
const { proxy } = getCurrentInstance();
import WebSocketClient from '@/js/utils/wsUtils';

// 定义websocket client对象
let _ws_client = null;

// 定义变量
let _key_event_monitor = true;
const _window_type = ref('main');

// 定义窗口中的快捷键
function monitorKeyEvent(monitor) {
    // console.log('monitorKeyEvent monitor = ', monitor);
    _key_event_monitor = monitor;
}

async function handleKeyDown(event) {
    // console.log('handleKeyDown _key_event_monitor = ', _key_event_monitor);
    if (!_key_event_monitor) {
        return;
    }
}

async function onWsMessage(message) {
    if (_window_type.value === 'map') {
        await proxy.$refs.capture_wnd.onWsMessage(message);
    } else if (_window_type.value === 'main') {
        await proxy.$refs.main_wnd.onWsMessage(message);
    }
}

onMounted(async () => {
    // 获取窗口类型和输入参数
    const urlParams = new URLSearchParams(window.location.search);
    _window_type.value = urlParams.get('windowType') || 'main';
    window.addEventListener('keydown', handleKeyDown);

    // 创建websocket client
    _ws_client = new WebSocketClient();
    _ws_client.init();
    _ws_client.addCallback(onWsMessage);
});

onUnmounted(() => {
    window.removeEventListener('keydown', handleKeyDown);
});
</script>

<style scoped></style>
